<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

abstract class m_backoffice_extends_View extends f_core_View
{
    public function getCurrentPage()
    {
        return f_core_Context::getInstance()->getContext("currentPage");
    }

    public function getCurrentWebsite()
    {
        return f_core_Context::getInstance()->getContext("currentWebsite");
    }

    public function getCurrentUser()
    {
        return f_context_User::getInstance()->getUser();
    }

    public function getTemplateEngine()
    {
        $tplEngine = parent::getTemplateEngine();

        if(!f_core_Context::getInstance()->isBackSession())
        {
            $tplEngine->setFrontTheme($this->getCurrentWebsite()->getTheme());
        }
        
        $tplEngine->assign('currentWebsite', $this->getCurrentWebsite());
        $tplEngine->assign('currentPage', $this->getCurrentPage());
        $tplEngine->assign('currentUser', $this->getCurrentUser());

        return $tplEngine;
    }

    	/**
	 * Generic method to render a view with model
	 * @param string $templateName
	 * @param boolean $withCache
	 */
	public function renderView($templateName, $withCache = false, $cacheDiscriminator = null, $cacheGroup = null, $debugMode = true)
	{
	    $tplEngine = $this->getTemplateEngine();

	    if($withCache === false)
	    {
            $tplEngine = $tplEngine->disableCache();
	    }

        if(!is_null($cacheDiscriminator))
        {
            $tplEngine->addCacheDiscriminator($cacheDiscriminator);
        }

        if(!is_null($cacheGroup))
        {
            $tplEngine->setCacheGroup($cacheGroup);
        }

	    $tplEngine->setModuleTemplate($this->getModuleName(), $templateName);

        $models = $this->getModels();

	    if(is_array($models) && count($models) > 0)
	    {
            foreach($models as $varName => $value)
            {
                $tplEngine->assign($varName, $value);
            }
	    }

	    $tplEngine->renderDisplay($debugMode);
	}
}